package com.jwsoft.manager.core.integration.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.AuthStatusEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.data.core.util.PagerUtil;
import com.bifang.module.base.core.helper.DictHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.enums.EduBlackListDictEnum;
import com.jwsoft.manager.common.enums.EduWorkOrderLogTypeDictEnum;
import com.jwsoft.manager.core.util.EduHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.convert.Convert;

import java.util.Arrays;
import java.util.List;

import com.bifang.engine.assistant.core.exception.AppException;
import org.springframework.util.ObjectUtils;
import org.springframework.transaction.annotation.Transactional;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.jwsoft.manager.common.vo.eduStudentEnrollBlacklist.EduStudentEnrollBlacklistVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollBlacklist.EduStudentEnrollBlacklistKeyVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollBlacklist.EduStudentEnrollBlacklistQueryVO;
import com.jwsoft.manager.core.integration.EduStudentEnrollBlacklistIntegration;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollBlacklistService;
import com.jwsoft.manager.core.dao.model.EduStudentEnrollBlacklist;

/**
 * 报名黑名单管理业务实现类
 *
 * @author chenzh
 * @since 2023-08-04
 */
@ApiService(funcCode = "enrollBlacklist", title = "报名黑名单管理")
public class EduStudentEnrollBlacklistIntegrationImpl implements EduStudentEnrollBlacklistIntegration {

    @Autowired
    public EduStudentEnrollBlacklistService eduStudentEnrollBlacklistService;
    @Autowired
    private EduHelper eduHelper;
    @Autowired
    private DictHelper dictHelper;

    @Override
    @OpApi(funcCode = "enrollBlacklist0001", title = "报名黑名单管理分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduStudentEnrollBlacklistVO> getList(EduStudentEnrollBlacklistQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        if (vo.getYear() == null) {
            vo.setYear(eduHelper.thisYear());
        }
        if (vo.getAreaCode() == null) {
            vo.setAreaCode(SessionUtil.getAreaCode());
        }
        QueryWrapper<EduStudentEnrollBlacklist> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(EduStudentEnrollBlacklist::getYear, vo.getYear())
                .eq(EduStudentEnrollBlacklist::getAreaCode, vo.getAreaCode())
                .eq(StringUtils.isNotBlank(vo.getIdcard()), EduStudentEnrollBlacklist::getIdcard, vo.getIdcard())
                .like(StringUtils.isNotBlank(vo.getFullName()), EduStudentEnrollBlacklist::getFullName, vo.getFullName())
                .eq(StringUtils.isNotBlank(vo.getBlacklistType()), EduStudentEnrollBlacklist::getBlacklistType, vo.getBlacklistType())
                .eq(StringUtils.isNotBlank(vo.getStatus()), EduStudentEnrollBlacklist::getStatus, vo.getStatus())
                .eq(StringUtils.isNotBlank(vo.getEncryptionFlag()), EduStudentEnrollBlacklist::getEncryptionFlag, vo.getEncryptionFlag())
                .orderByDesc(EduStudentEnrollBlacklist::getCreateTime, EduStudentEnrollBlacklist::getUpdateTime, EduStudentEnrollBlacklist::getFullName);

        List<EduStudentEnrollBlacklist> list = eduStudentEnrollBlacklistService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduStudentEnrollBlacklist> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduStudentEnrollBlacklistVO> resultList = Convert.toList(EduStudentEnrollBlacklistVO.class, pageInfo.getList());
        // 转义数据字典值
        dictHelper.valueToName(resultList, Arrays.asList(EduBlackListDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "enrollBlacklist0002", title = "报名黑名单管理根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduStudentEnrollBlacklistVO getById(EduStudentEnrollBlacklistKeyVO vo) {
        EduStudentEnrollBlacklist entity = eduStudentEnrollBlacklistService.getById(vo.getStudentEnrollBlacklistId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduStudentEnrollBlacklistVO result = Convert.convert(EduStudentEnrollBlacklistVO.class, entity);
        return result;
    }

    /**
     * 保存前统一校验
     */
    @Override
    @OpApi(funcCode = "enrollBlacklist0003", title = "报名黑名单管理保存前校验", funcType = FuncTypeEnum.other, checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduStudentEnrollBlacklistVO vo) {
        if (StringUtils.isEmpty(vo.getIdcard())) {
            throw new AppException("身份证不能为空");
        }
        if (StringUtils.isEmpty(vo.getFullName())) {
            throw new AppException("姓名不能为空");
        }
        if (StringUtils.isEmpty(vo.getAreaCode())) {
            throw new AppException("行政区划代码不能为空");
        }
        if (ObjectUtils.isEmpty(vo.getYear())) {
            throw new AppException("招生年份不能为空");
        }
        String idcard = vo.getIdcard().trim();
        vo.setIdcard(idcard);
    }

    @Override
    @OpApi(funcCode = "enrollBlacklist0004", title = "报名黑名单管理新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "enrollBlacklist0004",
            operationName = "报名黑名单管理新增",
            dataType = "studentEnrollBlacklistId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduStudentEnrollBlacklistVO add(EduStudentEnrollBlacklistVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        long count = eduStudentEnrollBlacklistService.count(Wrappers.<EduStudentEnrollBlacklist>lambdaQuery()
                .eq(EduStudentEnrollBlacklist::getYear, vo.getYear())
                .eq(EduStudentEnrollBlacklist::getIdcard, vo.getIdcard()));
        if (count > 0) {
            throw new AppException("该学生已经拉入了黑名单了，无法进行添加");
        }
        EduStudentEnrollBlacklist entity = Convert.convert(EduStudentEnrollBlacklist.class, vo);
        eduStudentEnrollBlacklistService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setStudentEnrollBlacklistId(entity.getStudentEnrollBlacklistId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "enrollBlacklist0005", title = "报名黑名单管理修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "enrollBlacklist0005",
            operationName = "报名黑名单管理修改",
            dataType = "studentEnrollBlacklistId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduStudentEnrollBlacklistVO edit(EduStudentEnrollBlacklistVO vo) {
        if (ObjectUtils.isEmpty(vo.getStudentEnrollBlacklistId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduStudentEnrollBlacklist oldEntity = eduStudentEnrollBlacklistService.getById(vo.getStudentEnrollBlacklistId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduStudentEnrollBlacklist entity = Convert.convert(EduStudentEnrollBlacklist.class, vo);
        eduStudentEnrollBlacklistService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "enrollBlacklist0006", title = "报名黑名单管理根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "enrollBlacklist0006",
            operationName = "报名黑名单管理删除",
            dataType = "studentEnrollBlacklistId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduStudentEnrollBlacklistKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getStudentEnrollBlacklistId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduStudentEnrollBlacklist entity = eduStudentEnrollBlacklistService.getById(vo.getStudentEnrollBlacklistId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduStudentEnrollBlacklistService.removeById(vo.getStudentEnrollBlacklistId());
    }
}
